﻿Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Imports System.Text

Public Class PTIC_DataGridView_V2
    Inherits DataGridView

#Region "Déclaration API"
    Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
    Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Integer, ByVal hDC As Integer) As Integer
    Declare Function setCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Integer
    Declare Function createRoundRectRgn Lib "gdi32" _
        (ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal x3 As Integer, ByVal y3 As Integer) As Integer
    Declare Function setWindowRgn Lib "user32" _
        (ByVal hwnd As IntPtr, ByVal hrgn As IntPtr, ByVal bRedraw As Boolean) As Integer
#End Region

#Region "Enumérations"
    Public Enum nomThemeDataGridView
        OMANE
        Standard
    End Enum
#End Region

#Region "Variables de classe"
    Private components As System.ComponentModel.IContainer
    Private oldRowIndex As Integer = 0
    Private _theme As ThemeComposant
    Private _nomTheme As nomThemeDataGridView
    Private _isSelectionable As Boolean

    Protected _PTICtopLeftCellComplementStyle As New complStyleCellule_PTICdgv
    Protected _PTICcolumnHeaderComplementStyle As New complStyleCellule_PTICdgv
    Protected _PTICrowHeaderComplementStyle As New complStyleCellule_PTICdgv
    Protected _PTICcellsComplementStyle As New complStyleCellule_PTICdgv
    Protected _PTICselectionBorderStyle As styleBordure

    Protected _columnHeaderMouseOver As Integer
    Protected _rowHeaderMouseOver As Integer

    Friend WithEvents ContextMenuStrip1 As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents ToolStripMenuItemExportExcel As System.Windows.Forms.ToolStripMenuItem

#End Region

#Region "Constructeur"
    Public Sub New()

        'Activation du double buffering
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)

        InitializeComponent()

        Me.CellBorderStyle = DataGridViewCellBorderStyle.None

        _isSelectionable = False
        oldRowIndex = 0
        defaut()
    End Sub
    Public Sub defaut()


        'ColumnHeaders
        Dim styleBorduresHeadersBase As styleBordure
        Dim penBorduresHeadersBase As Pen = New Pen(New SolidBrush(My.Settings.OMANE_ColorBorder), 1)
        penBorduresHeadersBase.DashStyle = DashStyle.Solid
        styleBorduresHeadersBase = New styleBordure(penBorduresHeadersBase)

        Dim styleBorduresHeadersSelected As styleBordure
        Dim penBorduresHeadersSelected As Pen = New Pen(New SolidBrush(My.Settings.OMANE_ColorBorder), 1)
        penBorduresHeadersSelected.DashStyle = DashStyle.Solid
        styleBorduresHeadersSelected = New styleBordure(penBorduresHeadersSelected)


        Dim remplissageHeadersBase As New remplissageDoubleDegrade(New CouleurDegrade(My.Settings.OMANELblSTD_ColorDegradeStandard1, _
                                                                                       My.Settings.OMANELblSTD_ColorDegradeStandard2), _
                                                                   New CouleurDegrade(My.Settings.OMANELblSTD_ColorDegradeStandard1, _
                                                                                       My.Settings.OMANELblSTD_ColorDegradeStandard2), _
                                                                   1)
        Dim remplissageHeadersSelection As New remplissageDoubleDegrade(New CouleurDegrade(Color.FromArgb(255, 227, 193), _
                                                                                           Color.FromArgb(255, 185, 109)), _
                                                                        New CouleurDegrade(My.Settings.OMANEBtnMOUSEOVER_ColorDegrade, _
                                                                                           My.Settings.OMANEBtnMOUSEOVER_ColorDegrade), _
                                                                        1)
        Dim remplissageHeadersMouseOver As New remplissageDoubleDegrade(New CouleurDegrade(Color.FromArgb(255, 227, 193), _
                                                                                           Color.FromArgb(255, 227, 193)), _
                                                                        New CouleurDegrade(Color.FromArgb(255, 227, 193), _
                                                                                           Color.FromArgb(255, 227, 193)), _
                                                                        1)
        Dim columnHeaderComplementStyleBase As New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersBase, styleBorduresHeadersBase)
        _PTICcolumnHeaderComplementStyle.styleBase = columnHeaderComplementStyleBase
        _PTICcolumnHeaderComplementStyle.styleMouseDown = New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersSelection, styleBorduresHeadersBase)
        _PTICcolumnHeaderComplementStyle.styleMouseOver = New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersMouseOver, styleBorduresHeadersBase)
        _PTICcolumnHeaderComplementStyle.styleSelection = New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersSelection, styleBorduresHeadersBase)
        _PTICcolumnHeaderComplementStyle.setStyleEnCours(complStyleCellule_PTICdgv.nomStyleEnCours.Base)

        'RowHeaders
        _PTICrowHeaderComplementStyle.styleBase = columnHeaderComplementStyleBase
        _PTICrowHeaderComplementStyle.styleMouseDown = New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersSelection, styleBorduresHeadersBase)
        _PTICrowHeaderComplementStyle.styleMouseOver = New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersMouseOver, styleBorduresHeadersBase)
        _PTICrowHeaderComplementStyle.styleSelection = New complStyleCelluleUnitaire_PTICdgv(remplissageHeadersSelection, styleBorduresHeadersSelected)
        _PTICrowHeaderComplementStyle.setStyleEnCours(complStyleCellule_PTICdgv.nomStyleEnCours.Base)

        'topLeftCell
        _PTICtopLeftCellComplementStyle.styleBase = columnHeaderComplementStyleBase
        _PTICtopLeftCellComplementStyle.styleMouseDown = columnHeaderComplementStyleBase
        _PTICtopLeftCellComplementStyle.styleMouseOver = columnHeaderComplementStyleBase
        _PTICtopLeftCellComplementStyle.styleSelection = columnHeaderComplementStyleBase
        _PTICtopLeftCellComplementStyle.setStyleEnCours(complStyleCellule_PTICdgv.nomStyleEnCours.Base)

        'Cells
        Dim styleBorduresCells As styleBordure
        'Dim penBorduresCells As Pen = New Pen(New SolidBrush(My.Settings.OMANE_ColorBorder), 1)
        Dim penBorduresCells As Pen = New Pen(New SolidBrush(My.Settings.OMANE_ColorBorder), 1)
        penBorduresCells.DashStyle = DashStyle.Dot
        styleBorduresCells = New styleBordure(penBorduresCells)
        _PTICcellsComplementStyle.styleBase = New complStyleCelluleUnitaire_PTICdgv(Nothing, styleBorduresCells)
        _PTICcellsComplementStyle.styleMouseDown = _PTICcellsComplementStyle.styleBase
        _PTICcellsComplementStyle.styleMouseOver = _PTICcellsComplementStyle.styleBase
        _PTICcellsComplementStyle.styleSelection = _PTICcellsComplementStyle.styleBase
        _PTICcellsComplementStyle.setStyleEnCours(complStyleCellule_PTICdgv.nomStyleEnCours.Base)

        'LE rectangle de sélection
        Dim penBorduresSelection As Pen = New Pen(New SolidBrush(My.Settings.OMANE_ColorBorder), 2)
        penBorduresSelection.DashStyle = DashStyle.Solid
        _PTICselectionBorderStyle = New styleBordure(penBorduresSelection)

        'La couleur de la selection
        DefaultCellStyle.SelectionBackColor = Color.FromArgb(255, 227, 193)
        DefaultCellStyle.SelectionForeColor = Color.Black

        _columnHeaderMouseOver = -1
        _rowHeaderMouseOver = -1

    End Sub

    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(PTIC_DataGridView_V2))
        Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
        Me.ToolStripMenuItemExportExcel = New System.Windows.Forms.ToolStripMenuItem
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.ContextMenuStrip1.SuspendLayout()
        CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'ContextMenuStrip1
        '
        Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItemExportExcel})
        Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
        Me.ContextMenuStrip1.Size = New System.Drawing.Size(137, 26)
        '
        'ToolStripMenuItemExportExcel
        '
        Me.ToolStripMenuItemExportExcel.Name = "ToolStripMenuItemExportExcel"
        Me.ToolStripMenuItemExportExcel.Size = New System.Drawing.Size(136, 22)
        Me.ToolStripMenuItemExportExcel.Text = "Export Excel"
        '
        'ImageList1
        '
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList1.Images.SetKeyName(0, "delete")
        '
        'PTIC_DataGridView_V2
        '
        Me.ContextMenuStrip = Me.ContextMenuStrip1
        Me.ContextMenuStrip1.ResumeLayout(False)
        CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Propriétés"
    <Browsable(True), Category("Thème")> _
    Public Property nomStyle() As nomThemeDataGridView

        Get
            Return _nomTheme
        End Get

        Set(ByVal value As nomThemeDataGridView)

            _nomTheme = value

            If value = nomThemeDataGridView.OMANE Then

                Dim styleDgv_OMANE_STD As _
                New styleEtatComposant(My.Settings.OMANE_Title2BackColor, _
                                       My.Settings.OMANE_FontBase, _
                                       My.Settings.OMANE_STDForeColor, _
                                       My.Settings.OMANE_ColorBorder, _
                                       Nothing)
                'Dim styleDgv_OMANE_STD As _
                'New styleEtatComposant(My.Settings.OMANEBtnSTD_ColorDegrade, _
                '                       My.Settings.OMANE_FontBase, _
                '                       Color.White, _
                '                       My.Settings.OMANE_ColorBorder, _
                '                       Nothing)

                Dim styleDgvOMANE_MOUSEOVER As _
                        New styleEtatComposant(My.Settings.OMANERdbMOUSEOVER_ColorDegrade1, _
                                               My.Settings.OMANERdbMOUSEOVER_ColorDegrade2, _
                                               My.Settings.OMANERdbMOUSEOVER_ColorDegrade3, _
                                               My.Settings.OMANERdbMOUSEOVER_ColorDegrade4, _
                                               My.Settings.OMANE_FontBase, _
                                               My.Settings.OMANEBtnSTD_ColorFore, _
                                               My.Settings.OMANE_ColorBorder, _
                                               Nothing)
                Dim styleDgvOMANE_DIS As _
                        New styleEtatComposant(Nothing, _
                                               Nothing, _
                                               Nothing, _
                                               Nothing, _
                                               Nothing)

                _theme = New ThemeComposant(styleDgv_OMANE_STD, _
                                            styleDgvOMANE_MOUSEOVER, _
                                            styleDgvOMANE_DIS, etatComposant.Enabled)

            End If

            Invalidate()

        End Set

    End Property
    <Browsable(True), Category("Thème")> _
    Public Property isSelectionable() As Boolean
        Get
            Return _isSelectionable
        End Get
        Set(ByVal value As Boolean)
            _isSelectionable = value
        End Set
    End Property
#End Region

#Region "Dessin"


    ''Private Sub PTIC_DataGridView_V2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    '  SelectedCells(0).Size
    ''    For Each cell As DataGridViewCell In SelectedCells
    ''        If cell.Displayed Then
    ''            If cell.RowIndex <> -1 Then
    ''                e.Graphics.DrawRectangle(Pens.AliceBlue, 10, 10, 100, 100)
    ''            End If
    ''        End If
    ''    Next
    ''End Sub
    Private Sub PTIC_DataGridView_V2_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles Me.RowPostPaint

        If Me.Rows(e.RowIndex).ErrorText IsNot String.Empty Then
            Dim g As Graphics = e.Graphics
            g.DrawImage(Me.ImageList1.Images(0), 14, CInt(((e.RowBounds.Top + e.RowBounds.Bottom) - 16) / 2))
            'On récupère la hauteur de la ligne
            'On centre le dessin de l'icône d'erreur
            'On met le text dans la propriété adéquate de la cellule de titre de ligne
        End If

    End Sub
    Private Sub PTIC_DataGridView_RowPrePaint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles Me.RowPrePaint

        'Désactive le rafraichissement automatique du rectangle de focus
        e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus
        e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Border

        'Test si la ligne est sélectionnée ou non - dans l'affirmative, les cellules doivent être
        'repeintes avec la couleur de sélection personnalisé
        ''If (e.State And DataGridViewElementStates.Selected) = _
        ''    DataGridViewElementStates.Selected Then

        'Calcule les dimmensions de la ligne (en prenant en compte le scrolling horizontale
        '(dépassement de la dgv par la droite)
        ''Dim rowBounds As New Rectangle( _
        ''    Me.RowHeadersWidth, e.RowBounds.Top, _
        ''    Me.Columns.GetColumnsWidth( _
        ''    DataGridViewElementStates.Visible) - _
        ''    Me.HorizontalScrollingOffset, _
        ''    e.RowBounds.Height)

        ''Try
        'Remplissage de la ligne avec un dégradé
        ''Styles.PaintDoubleDegrade(g, rowBounds, _theme.styleMouseOver.backColor, _
        ''                          _theme.styleMouseOver.backColor2, _
        ''                          _theme.styleMouseOver.backColor3, _
        ''                          _theme.styleMouseOver.backColor4, 0.5)
        ''Finally
        ''End Try
        ''End If
        ''End If
    End Sub
    Private Sub PTIC_DataGridView_CellPainting(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Me.CellPainting

        'Caractéristiques de la cellule
        Dim g As Graphics = e.Graphics
        Dim rowIndex As Integer = e.RowIndex, columnIndex As Integer = e.ColumnIndex
        Dim cellBounds As Rectangle = e.CellBounds

        'Compléments de style associé à la cellule
        Dim complStyleCellule As complStyleCelluleUnitaire_PTICdgv = getComplementStyle(rowIndex, columnIndex)

        'Dessin de l'arrière plan et de la valeur de la cellule
        If Not complStyleCellule.remplissageArrierePlan Is Nothing Then

            Try

                'Remplissage de l'arrière plan
                If Not complStyleCellule.remplissageArrierePlan Is Nothing Then complStyleCellule.remplissageArrierePlan.paint(g, cellBounds)

                'Remplissage des valeurs des lignes de titre
                If (rowIndex <> -1 Or columnIndex <> -1) Then

                    Dim valeur As Object
                    Dim cellStyle As DataGridViewCellStyle

                    If columnIndex = -1 Then
                        valeur = rowIndex + 1
                        If rowIndex = RowCount - 1 And Me.AllowUserToAddRows Then valeur = "*"
                        cellStyle = RowHeadersDefaultCellStyle
                        'Calcul de la taille de la colonne de titre des lignes
                        Dim rowNumWidth As Integer = Me.RowCount.ToString().Length
                        Dim rowNumber As StringBuilder = New StringBuilder(rowNumWidth)
                        rowNumber.Append(e.RowIndex + 1)
                        While rowNumber.Length < rowNumWidth
                            rowNumber.Insert(0, "0")
                        End While
                        Dim Sz As SizeF = g.MeasureString(rowNumber.ToString, cellStyle.Font)
                        If RowHeadersWidth < Sz.Width + 20 Then RowHeadersWidth = Sz.Width + 20

                    ElseIf rowIndex = -1 Then
                        valeur = Columns(columnIndex).HeaderText
                        cellStyle = ColumnHeadersDefaultCellStyle

                    Else
                        valeur = Rows(rowIndex).Cells(columnIndex).Value
                        cellStyle = Me.Rows(rowIndex).Cells(columnIndex).InheritedStyle

                    End If
                    paintValeur(g, cellBounds, cellStyle, valeur)
                End If


                'Les bordures des lignes de titre
                'Les titres de colonne
                If rowIndex = -1 Then
                    complStyleCellule.borderStyle.paint(g, cellBounds)
                End If

                'Les titres de ligne
                ''If columnIndex = -1 And rowIndex <> -1 Then
                If rowIndex <> -1 Then
                    complStyleCellule.borderStyle.paint(g, cellBounds)
                    Dim lpen As Pen
                    If rowIndex < Rows.Count - 1 Then
                        If Me.Rows(rowIndex).Selected And Me.Rows(rowIndex + 1).Selected Then
                            lpen = _PTICrowHeaderComplementStyle.styleSelection.borderStyle.pen
                        Else
                            lpen = _PTICrowHeaderComplementStyle.styleBase.borderStyle.pen
                        End If
                    Else
                        lpen = _PTICrowHeaderComplementStyle.styleBase.borderStyle.pen
                    End If
                    g.DrawLine(lpen, cellBounds.Left, _
                            cellBounds.Bottom - 1, cellBounds.Right - 1, _
                            cellBounds.Bottom - 1)
                End If

                e.Handled = True

            Finally
            End Try
        Else
            ''Dim cellStyle As DataGridViewCellStyle = ColumnHeadersDefaultCellStyle
            e.PaintBackground(cellBounds, True)
            e.Paint(cellBounds, DataGridViewPaintParts.ContentForeground)
            Dim lpen As Pen = _PTICcellsComplementStyle.styleEnCours.borderStyle.pen
            g.DrawLine(lpen, cellBounds.Left, _
                    cellBounds.Bottom - 1, cellBounds.Right - 1, _
                    cellBounds.Bottom - 1)
            If cellBounds.Right - 1 >= Me.RowHeadersWidth Then
                g.DrawLine(lpen, cellBounds.Right - 1, _
                    cellBounds.Top, cellBounds.Right - 1, _
                    cellBounds.Bottom - 1)
            End If
            If Rows(rowIndex).Cells(columnIndex).InheritedStyle.SelectionBackColor <> Color.White Then dessineRectangleSelection(g, rowIndex, columnIndex, cellBounds)
            e.Handled = True

        End If
    End Sub


    Protected Sub paintValeur(ByVal g As Graphics, _
                              ByVal cellBounds As Rectangle, _
                              ByVal styleCellule As DataGridViewCellStyle, _
                              ByVal valeurCellule As Object)

        'Remplissage des valeurs
        If (valeurCellule IsNot Nothing) Then
            Dim rectText As Rectangle
            With styleCellule.Padding
                rectText = New Rectangle(cellBounds.X + .Left, _
                                         cellBounds.Y + .Top, _
                                         cellBounds.Width - .Left - .Right - 2, _
                                         cellBounds.Height - .Top - .Bottom)
            End With
            Dim stringFormat As New StringFormat()
            stringFormat.Alignment = StringAlignment.Near
            stringFormat.LineAlignment = StringAlignment.Center
            g.DrawString(Format(valeurCellule, styleCellule.Format), _
                         styleCellule.Font, _
                         New SolidBrush(styleCellule.ForeColor), _
                         rectText, _
                         stringFormat)
        End If
    End Sub
    Protected Sub dessineRectangleSelection(ByVal graphics As Graphics, ByVal rowIndex As Integer, ByVal columnIndex As Integer, ByVal cellBounds As Rectangle)

        'Récupération de la colonne affichée à droite de la colonne columnIndex
        Dim colAfficheePost As DataGridViewColumn = Me.Columns.GetNextColumn(Me.Columns(columnIndex), _
                                                                             DataGridViewElementStates.Visible, _
                                                                             DataGridViewElementStates.None)
        Dim colAfficheePrec As DataGridViewColumn = Me.Columns.GetPreviousColumn(Me.Columns(columnIndex), _
                                                                             DataGridViewElementStates.Visible, _
                                                                             DataGridViewElementStates.None)

        'Encadrement bas de la zone sélectionnée
        If Me.Rows(rowIndex).Cells(columnIndex).Selected Then

            With cellBounds

                'Dessin de la bordure basse
                Dim bordsBas As Boolean = False
                If rowIndex = Me.Rows.Count - 1 Then bordsBas = True Else If Not Me.Rows(rowIndex + 1).Cells(columnIndex).Selected Then bordsBas = True
                If bordsBas And .Right > Me.RowHeadersWidth Then graphics.DrawLine(_PTICselectionBorderStyle.pen, Math.Max(RowHeadersWidth, .Left), .Bottom - 1, .Right, .Bottom - 1)

                'Dessin de la bordure droite
                Dim bordsDroite As Boolean = False
                If colAfficheePost Is Nothing Then bordsDroite = True Else If Not Me.Rows(rowIndex).Cells(colAfficheePost.Index).Selected Then bordsDroite = True
                If bordsDroite And .Right > Me.RowHeadersWidth Then graphics.DrawLine(_PTICselectionBorderStyle.pen, .Right - 1, .Top, .Right - 1, .Bottom - 1)

                'Dessin de la bordure gauche
                Dim bordsGauche As Boolean = False
                If colAfficheePrec Is Nothing Then bordsGauche = True Else If Not Me.Rows(rowIndex).Cells(colAfficheePrec.Index).Selected Then bordsGauche = True
                If bordsGauche And .Left >= Me.RowHeadersWidth Then graphics.DrawLine(_PTICselectionBorderStyle.pen, .Left, .Top, .Left, .Bottom - 1)

                'Dessin de la bordure haute
                Dim bordsHaut As Boolean = False
                If rowIndex = 0 Then bordsHaut = True Else If Not Me.Rows(rowIndex - 1).Cells(columnIndex).Selected Then bordsHaut = True
                If bordsHaut And .Right > Me.RowHeadersWidth Then graphics.DrawLine(_PTICselectionBorderStyle.pen, Math.Max(RowHeadersWidth, .Left), .Top, .Right - 1, .Top)

            End With
        End If

    End Sub
    Protected Function getComplementStyle(ByVal rowIndex As Integer, ByVal columnIndex As Integer) As complStyleCelluleUnitaire_PTICdgv
        'Compléments de style associé à la cellule
        Dim complStyleCellule As complStyleCelluleUnitaire_PTICdgv

        If rowIndex = -1 And columnIndex = -1 Then
            complStyleCellule = _PTICtopLeftCellComplementStyle.styleEnCours

        ElseIf rowIndex = -1 Then
            If Me.Columns(columnIndex).Selected Then
                complStyleCellule = _PTICcolumnHeaderComplementStyle.styleSelection
            ElseIf columnIndex = _columnHeaderMouseOver Then
                complStyleCellule = _PTICcolumnHeaderComplementStyle.styleMouseOver
            Else
                complStyleCellule = _PTICcolumnHeaderComplementStyle.styleBase
            End If

        ElseIf columnIndex = -1 Then
            If Me.Rows(rowIndex).Selected Then
                complStyleCellule = _PTICrowHeaderComplementStyle.styleSelection
            ElseIf rowIndex = _rowHeaderMouseOver Then
                complStyleCellule = _PTICrowHeaderComplementStyle.styleMouseOver
            Else
                complStyleCellule = _PTICrowHeaderComplementStyle.styleBase
            End If

        Else
            complStyleCellule = _PTICcellsComplementStyle.styleEnCours

        End If
        Return complStyleCellule
    End Function

#End Region

#Region "Gestion des évènements de la dataGridView"

    Private Sub PTIC_DataGridView_V2_CellStateChanged(ByVal sender As Object, _
                                                      ByVal e As System.Windows.Forms.DataGridViewCellStateChangedEventArgs) Handles Me.CellStateChanged
        'MAJ dans cet élément du rectangle de sélection
        If e.StateChanged = DataGridViewElementStates.Selected Then
            Dim cellule As DataGridViewCell = e.Cell
            Dim rect As Rectangle = GetCellDisplayRectangle(cellule.ColumnIndex, cellule.RowIndex, False)
            rect.X = rect.X - _PTICselectionBorderStyle.pen.Width
            rect.Y = rect.Y - _PTICselectionBorderStyle.pen.Width
            rect.Width = rect.Width + _PTICselectionBorderStyle.pen.Width * 2
            rect.Height = rect.Height + _PTICselectionBorderStyle.pen.Width * 2
            Invalidate(rect)
        End If
    End Sub
    Private Sub PTIC_DataGridView_V2_ColumnStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnStateChangedEventArgs) Handles Me.ColumnStateChanged
        If e.StateChanged = DataGridViewElementStates.Selected Then
            Dim column As DataGridViewColumn = e.Column
            Dim rect As Rectangle = GetColumnDisplayRectangle(column.Index, True)
            rect.X = rect.X - _PTICselectionBorderStyle.pen.Width
            rect.Y = rect.Y - _PTICselectionBorderStyle.pen.Width
            rect.Width = rect.Width + _PTICselectionBorderStyle.pen.Width * 2
            rect.Height = rect.Height + _PTICselectionBorderStyle.pen.Width * 2
            Invalidate(rect)
        End If
    End Sub
    Private Sub PTIC_DataGridView_V2_RowStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles Me.RowStateChanged
        If e.StateChanged = DataGridViewElementStates.Selected Then
            Dim row As DataGridViewRow = e.Row
            Dim rect As Rectangle = GetRowDisplayRectangle(row.Index, True)
            rect.X = rect.X - _PTICselectionBorderStyle.pen.Width
            rect.Y = rect.Y - _PTICselectionBorderStyle.pen.Width
            rect.Width = rect.Width + _PTICselectionBorderStyle.pen.Width * 2
            rect.Height = rect.Height + _PTICselectionBorderStyle.pen.Width * 2
            Invalidate(rect)
        End If
    End Sub

    Private Sub PTIC_DataGridView_V2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

        Dim x_pos As Integer, y_pos As Integer
        Dim objMousePosition As Point = Me.PointToClient(Control.MousePosition)
        Dim objHitTestInfo As DataGridView.HitTestInfo

        'Récupération de la position du click dans les coordonnées de la dataGridView
        objHitTestInfo = Me.HitTest(objMousePosition.X, objMousePosition.Y)
        x_pos = objHitTestInfo.RowIndex
        y_pos = objHitTestInfo.ColumnIndex


        Dim numColumnHeaderSelectione As Integer
        If x_pos = -1 Then
            numColumnHeaderSelectione = y_pos
        Else
            numColumnHeaderSelectione = -1
        End If
        If numColumnHeaderSelectione <> _columnHeaderMouseOver Then
            _columnHeaderMouseOver = numColumnHeaderSelectione
            Me.Invalidate()
        End If


        Dim numRowHeaderSelectione As Integer
        If y_pos = -1 Then
            numRowHeaderSelectione = x_pos
        Else
            numRowHeaderSelectione = -1
        End If
        If numRowHeaderSelectione <> _rowHeaderMouseOver Then
            _rowHeaderMouseOver = numRowHeaderSelectione
            Me.Invalidate()
        End If


    End Sub
    Public Sub PTIC_DataGridView_V2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Dim x_pos As Integer, y_pos As Integer
        Dim objMousePosition As Point = Me.PointToClient(Control.MousePosition)
        Dim objHitTestInfo As DataGridView.HitTestInfo

        'Récupération de la position du click dans les coordonnées de la dataGridView
        objHitTestInfo = Me.HitTest(objMousePosition.X, objMousePosition.Y)
        x_pos = objHitTestInfo.RowIndex
        y_pos = objHitTestInfo.ColumnIndex

        If x_pos = -1 And y_pos <> -1 Then
            If Me.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect Then
                For Each c As DataGridViewColumn In Me.Columns
                    c.SortMode = DataGridViewColumnSortMode.Programmatic
                Next
                Me.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
                Me.Columns(y_pos).Selected = True
            End If
        End If
        If y_pos = -1 And x_pos <> -1 Then
            If Me.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect Then
                Me.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
                Me.Rows(x_pos).Selected = True
            End If
        End If
    End Sub
    Private Sub PTIC_DataGridView_V2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        _columnHeaderMouseOver = -1
        _rowHeaderMouseOver = -1
        Me.Invalidate()
    End Sub

    'UserControl remplace la méthode Dispose pour nettoyer la liste des composants.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

#End Region

#Region "Gestion des évènements du MenuStrip"
    Private Sub ToolStripMenuItemExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripMenuItemExportExcel.Click
        PTIC_Util.exportExcel(Me.DataSource)
    End Sub
#End Region

#Region "Structures encapsulées"
    Public Class complStyleCelluleUnitaire_PTICdgv

        Protected _remplissageArrierePlan As couleurRemplissage
        Protected _borderStyle As styleBordure

        Public Sub New(ByVal remplissageArrierePlan As couleurRemplissage, _
                       ByVal borderStyle As styleBordure)
            _remplissageArrierePlan = remplissageArrierePlan
            _borderStyle = borderStyle
        End Sub
        Public Property remplissageArrierePlan() As couleurRemplissage
            Get
                Return _remplissageArrierePlan
            End Get
            Set(ByVal value As couleurRemplissage)
                _remplissageArrierePlan = value
            End Set
        End Property
        Public Property borderStyle() As styleBordure
            Get
                Return _borderStyle
            End Get
            Set(ByVal value As styleBordure)
                _borderStyle = value
            End Set
        End Property

    End Class
    Public Class complStyleCellule_PTICdgv
        Protected _complStyleBase As complStyleCelluleUnitaire_PTICdgv
        Protected _complStyleSelection As complStyleCelluleUnitaire_PTICdgv
        Protected _complStyleMouseOver As complStyleCelluleUnitaire_PTICdgv
        Protected _complStyleMouseDown As complStyleCelluleUnitaire_PTICdgv
        Protected _complStyleEnCours As complStyleCelluleUnitaire_PTICdgv
        Public Property styleBase() As complStyleCelluleUnitaire_PTICdgv
            Get
                Return _complStyleBase
            End Get
            Set(ByVal value As complStyleCelluleUnitaire_PTICdgv)
                _complStyleBase = value
            End Set
        End Property
        Public Property styleSelection() As complStyleCelluleUnitaire_PTICdgv
            Get
                Return _complStyleSelection
            End Get
            Set(ByVal value As complStyleCelluleUnitaire_PTICdgv)
                _complStyleSelection = value
            End Set
        End Property
        Public Property styleMouseOver() As complStyleCelluleUnitaire_PTICdgv
            Get
                Return _complStyleMouseOver
            End Get
            Set(ByVal value As complStyleCelluleUnitaire_PTICdgv)
                _complStyleMouseOver = value
            End Set
        End Property
        Public Property styleMouseDown() As complStyleCelluleUnitaire_PTICdgv
            Get
                Return _complStyleMouseDown
            End Get
            Set(ByVal value As complStyleCelluleUnitaire_PTICdgv)
                _complStyleMouseDown = value
            End Set
        End Property
        Public ReadOnly Property styleEnCours() As complStyleCelluleUnitaire_PTICdgv
            Get
                Return _complStyleEnCours
            End Get
        End Property
        Friend Sub setStyleEnCours(ByVal nomStyleEnCours As nomStyleEnCours)
            Select Case nomStyleEnCours
                Case complStyleCellule_PTICdgv.nomStyleEnCours.Selection
                    _complStyleEnCours = _complStyleSelection
                Case complStyleCellule_PTICdgv.nomStyleEnCours.MouseDown
                    _complStyleEnCours = _complStyleMouseDown
                Case complStyleCellule_PTICdgv.nomStyleEnCours.MouseOver
                    _complStyleEnCours = _complStyleMouseOver
                Case Else
                    _complStyleEnCours = _complStyleBase
            End Select
        End Sub
        Friend Enum nomStyleEnCours
            Base
            MouseOver
            MouseDown
            Selection
        End Enum
    End Class
#End Region

End Class
